package com.lianqi.emcpframework.core.dict.internal;

import com.lianqi.emcpframework.core.dict.DictParser;
import com.lianqi.emcpframework.core.dict.Dictionary;
import com.lianqi.emcpframework.core.dict.EnumDict;

import java.util.Collections;
import java.util.stream.Collectors;

/**
 * @program emcp-framework
 * @ClassName EnumDictParser
 * @description: 枚举类型字典解析器
 * @author: sky
 * @create: 2018/11/23 10:05
 */
public class EnumDictParser implements DictParser{
    @Override
    public String getId() {
        return "default";
    }

    @Override
    public String paserText(Dictionary define, Object value) {
        return define.getItems()
                .stream()
                .filter(item -> item.eq(value))
                .map(EnumDict :: getText)
                .collect(Collectors.joining(","));
    }

    @Override
    public String parserValue(Dictionary define, String text) {
        return define.getItems()
                .stream()
                .filter(item -> item.eq(text))
                .map(EnumDict :: getValue)
                .map(String :: valueOf)
                .findFirst()
                .orElse(text);
    }
}
